package Beans.controllers;

import java.util.ArrayList;
import java.util.Map;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import Beans.RecipeBean;
import Beans.RecipeListModelBean;
import Beans.SearchRecipesBean;
import step4.dao.fabric.DaoFabric;
import step4.dao.instance.RecipesDao;

@ManagedBean
@ApplicationScoped 
public class SearchRecipeBeanController {
	private RecipesDao recipeDAO;

	public SearchRecipeBeanController() {
		this.recipeDAO = DaoFabric.getInstance().createRecipesDao();
	}
	
	public void seachRecipe(SearchRecipesBean infos) {
		ArrayList<RecipeBean> list = this.recipeDAO.getRecipesByInfos(infos);
		RecipeListModelBean recipeList = new RecipeListModelBean();
		
		for(RecipeBean recipe:list){
			recipeList.addRecipeList(recipe);
		}
		
		//r�cup�re l'espace de m�moire de JSF
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		
		//place la liste de recette dans l'espace de m�moire de JSF
		sessionMap.put("recipesListSorted", recipeList);
	}
	
	public void loadAllRecipe(){
		ArrayList<RecipeBean> list = this.recipeDAO.getAllRecipes();
		
		RecipeListModelBean recipeList=new RecipeListModelBean();
		
		for(RecipeBean recipe:list){
			recipeList.addRecipeList(recipe);
		}
		
		//r�cup�re l'espace de m�moire de JSF
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		
		//place la liste de recette dans l'espace de m�moire de JSF
		sessionMap.put("recipeList", recipeList);
		
	}
}
